Skip to content

perf: use klauspost/compress for gzip and flate decompression - #19

Open
burruplambert wants to merge 1 commit into
bogdanfinn:masterfrom
burruplambert:perf/klauspost-decompression
Open

perf: use klauspost/compress for gzip and flate decompression#19
burruplambert wants to merge 1 commit into
bogdanfinn:masterfrom
burruplambert:perf/klauspost-decompression

Conversation

@burruplambert

Copy link
Copy Markdown

Drop-in replacement of compress/gzip and compress/flate with github.com/klauspost/compress for response body decompression.

The klauspost implementation has a significantly faster Huffman decoder. In CPU profiling of a high-throughput production client workload, stdlib gzip decompression accounted for roughly 5% of total CPU, and swapping the import removed most of that.

  • No API or wire behavior change - the klauspost gzip and flate packages are drop-in compatible with the stdlib ones
  • No new dependency - the module already requires klauspost/compress
  • Supersedes the decompression half of perf: reduce CPU overhead in gzip decompression and header lowercasing #17, which bundled two unrelated changes and is being closed in favor of focused PRs

Note: the package test suite reports a failure at the end of any full run because TestMain's goroutine leak checker flags its own goroutine. That is pre-existing on master and unrelated to this change.

The stdlib compress/gzip and compress/flate use a slower Huffman
decoder. klauspost/compress is a drop-in replacement with a faster
implementation, measured at ~5% total CPU reduction in production
profiling of a high-throughput HTTP client workload.
@burruplambert
burruplambert force-pushed the perf/klauspost-decompression branch from e0b3bdd to 972a168 Compare August 1, 2026 13:06
@burruplambert

Copy link
Copy Markdown
Author

Checked the drop-in claim rather than relying on it. Compared klauspost against stdlib across 14 malformed-input cases - empty input, non-gzip data, truncated header, truncated body, truncated trailer, bad magic, bad compression method, corrupt CRC, corrupt size, corrupt deflate stream, trailing garbage, and concatenated streams, plus a corrupt raw flate stream.

Every case produced the identical outcome, including the exact error text - gzip: invalid header, gzip: invalid checksum, unexpected EOF, and even flate: corrupt input before offset 25. No case differed, and neither implementation panicked where the other did not.

So callers that inspect or match on decompression errors see no change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant